<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller;

use Swoft\Http\Message\ContentType;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\View\Annotation\Mapping\View;
use Swoft\Co;
use Throwable; 

// use Swoft\Annotation\AnnotationRegister;
use App\Http\Controller\RpcController;
// use App\Http\Controller\MyAnnotation;
use App\Http\Controller\BreakerController;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use \ReflectionClass;
use \ReflectionMethod;


class Foo
{
    /**
     * @MyAnnotation(myProperty="value11")
     */
    private $bar;


    /**
     * @MyAnnotation(myList="myList")
     */
    public function getList():array
    {
        return ["aa"=>11];
    }

}

/**
 * @Annotation
 */
final class MyAnnotation
{
    public $myProperty;
    public $myList;
}

/**
 * Class ViewController
 *
 * @since 2.0
 *
 * @Controller(prefix="view")
 */
class ViewController 
{
    // private $myProperty = "111";

    /**
     * @RequestMapping("annotation")
     *
     * @param Response $response
     *
     * @return Response 
     */
    public function annotation(Response $response): Response
    {
        // Deprecated and will be removed in 2.0 but currently needed
        AnnotationRegistry::registerLoader('class_exists');

        $reflectionClass = new ReflectionClass(Foo::class);
        $property = $reflectionClass->getProperty('bar');
        // $property = $reflectionClass->getMethod('getList');

        $reader = new AnnotationReader();
        $myAnnotation = $reader->getPropertyAnnotation($property, MyAnnotation::class);
        // $myAnnotation = $reader->getFunctionAnnotation($property, MyAnnotation::class);

        echo $myAnnotation->myProperty; // result: "value"
        $result = ["result"=>$myAnnotation->myProperty];

        $response = $response->withContent( \json_encode($result) );

        return $response;
    }


    /**
     * @RequestMapping("index")
     *
     * @param Response $response
     *
     * @return Response 
     */
    public function index(Response $response): Response
    {
        $id = Co::id();
        $calc = $this->getCacle();
        $response = $response->withContent($calc.'Laravel framework 111  '.$id);
        // $response = $response->withContentType(ContentType::HTML);
        return $response;
    }


    public function getCacle(): float
    {
        $a = 1.3;
        $b = "2.4";
        return (string)$a + $b;
    }

    /**
     * Will render view by annotation tag View
     *
     * @RequestMapping("/home")
     * @View("home/index")
     *
     * @throws Throwable
     */
    public function indexByViewTag(): array
    {
        return [
            'msg' => 'hello'
        ];
    }
}
